package com.android.xcalendar.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

import com.android.xcalendar.R;
import com.android.xcalendar.util.XCalendarAPIManager;
import com.android.xcalendar.util.XCalendarContentManager;

public class NewLocationActivity extends Activity {
	
	double latitude, longitude = 0.0;
	Button btnAddLocation, btnGetLocation;
	EditText editLongitude, editLatitude, editLocationName;
	String url;
	XCalendarContentManager contentManager = XCalendarContentManager.getInstance();
	XCalendarAPIManager apiManager = XCalendarAPIManager.getInstance();	
	private ProgressDialog pDialog;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.new_location_view);
	    
	    btnGetLocation = (Button) findViewById(R.id.button_getLocation);
	    btnAddLocation = (Button) findViewById(R.id.button_createLocation);
	    editLatitude = (EditText) findViewById(R.id.editLatitude);
	    editLongitude = (EditText) findViewById(R.id.editLongitude);
	    editLocationName = (EditText) findViewById(R.id.editLocationName);
	    pDialog = new ProgressDialog(this);
	    
	    btnGetLocation.setOnClickListener(getLocationListener);	
	    btnAddLocation.setOnClickListener(createLocationListener);
	    // TODO Auto-generated method stub
	}
	
	private OnClickListener getLocationListener = new OnClickListener()
	{
		@Override
		public void onClick(View v) {
			LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
			if(locationManager.isProviderEnabled(locationManager.GPS_PROVIDER)){
				Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				if(location != null){
					latitude = location.getLatitude();
					longitude = location.getLongitude();
				}
				else{
					LocationListener locationListener = new LocationListener() {
						
						@Override
						public void onStatusChanged(String provider, int status, Bundle extras) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onProviderEnabled(String provider) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onProviderDisabled(String provider) {
							// TODO Auto-generated method stub
							
						}
						
						@Override
						public void onLocationChanged(Location location) {
							// TODO Auto-generated method stub
							
							
						}
					};
					locationManager.requestLocationUpdates(locationManager.NETWORK_PROVIDER, 1000, 0, locationListener);
					if (location != null){
						latitude = location.getLatitude();
						longitude = location.getLongitude();
					}
				}
				editLatitude.setText(Double.toString(latitude));
				editLongitude.setText(Double.toString(longitude));
			}
			
		}
	};
	
	private OnClickListener createLocationListener = new OnClickListener()
	{
		@Override
		public void onClick(View v) {
						
			url = "http://xiaoyitest.appspot.com/resources/AddLocation/userName="
				+ contentManager.getUserName()
				+ "&locationName=" + editLocationName.getText()
				+ "&latitude=" + Double.toString(latitude)
				+ "&longitude=" + Double.toString(longitude)+ "/";
			new AddLocationAsyncTask().execute(url) ;
		}
	};
	
	private class AddLocationAsyncTask extends AsyncTask<String, String, String> {
		protected void onPreExecute() {
			if (pDialog != null) {
				pDialog = ProgressDialog.show(NewLocationActivity.this, "", "Creating new location. Please wait...", true);
				pDialog.setCancelable(true);
				pDialog.setOnCancelListener(new OnCancelListener() {

					@Override
					public void onCancel(DialogInterface dialog) {
						cancel(true);
						pDialog.dismiss();
					}
				});
			}
			
		}

		protected String doInBackground(String... urls) {
			apiManager.connect(urls[0]);
			return "";
		}

		protected void onPostExecute(String result) {
			pDialog.dismiss();
			finish();
		}
	}

}
